import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_utils/api_service/acg_service.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../hj_model/cartoon_media_info.dart';

abstract class TagDetailBaseController extends GetxController {
  TagsBean? tagModel;

  TagDetailBaseController(this.tagModel);

  bool isLoading = true;
  final bool showChangeButton = true;
  RefreshController? refreshController;
  final dataSource = [];
  int page = 1;
  int videoType = 0;
  double childRatio = 191 / 174;
  double crossAxisSpacing = 10.w;
  double mainAxisSpacing = 10.h;
  int sortIndex = 0;
  final sorts = ['最多观看', '最新上架', '最多喜欢'];

  int crossAxisCount = 2;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    fetchTagDetail().then((value) => fetchPageData());
  }

  Future<TagsBean?> fetchTagDetail();

  Widget instanceChildItem(int index);

  changeLoadStyle() => UnimplementedError;

  fetchPageData({bool isRefresh = true, bool showLoading = false});
}

class CartoonTagLogic extends GetxController {
  String? sId;

  CartoonTagLogic(this.sId);

  int page = 1;
  bool isLoading = true;
  RefreshController? refreshController;
  List<CartoonMediaInfo> dataSource = [];
  int sortIndex = 0;
  final sorts = ['最多观看', '最新上架', '最多喜欢'];
  final sortParam = [1, 2, 3];

  @override
  void onInit() {
    fetchPageData();
    super.onInit();
  }

  fetchPageData({bool isRefresh = true, bool showLoading = false}) async {
    if (isRefresh) {
      page = 1;
    }
    if (showLoading) {
      isLoading = true;
      update();
    }
    // final res = await ACGService.fetchMediaByTagId(cId ?? '', page: page, size: 15, sort: ['play', 'new', 'like'][sortIndex]);
    final res = await ACGService.getMoreCartoonData(page, 12, sortParam[sortIndex], sId ?? '');
    if (isRefresh) {
      refreshController?.refreshCompleted();
      dataSource.clear();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.list ?? []);
    page += 1;

    isLoading = false;
    update();
  }

  Future<TagsBean?> fetchTagDetail() async {
    final res = await ACGService.fetchMediaTagInfo(sId ?? '');
    if (res != null) {
      update();
    }
  }
}
